#include "console.h"

#include <string>
#include <vector>

using namespace std;

int main(int argc, char** argv) {
	const int width = 120;
	const int height = 80;
	Console c("Uga Demo", width, height);

	struct StringsWithAttr {
		string str;
		Position pos;
		int attr;
	};

	vector<StringsWithAttr> elem;
	vector<Input> inputs;
	int current_row = 0;
	for (;;) {
		c.ReadInputs(inputs);

		const string s(1 + (rand() % 20), char(rand() % 256));
		for (const auto& i : inputs) {
			switch (i.type_) {
			case INPUT_MOUSE_LCLICK:
				if (i.content_ >= 0) {
					elem.erase(elem.begin() + i.content_);
				}
				break;

			case INPUT_MOUSE_CCLICK:
				if (i.content_ >= 0) {
					elem[i.content_].attr = rand() % 256;
				}
				break;

			case INPUT_MOUSE_RCLICK:
				elem.push_back(StringsWithAttr{s, i.position_, rand() % 256 });
				break;

			case INPUT_KEY_PRESSED:
				elem.push_back(
					StringsWithAttr{ 
						string(1 + (rand() % 20), char(i.content_)),
						Position{current_row, 0},
						rand() % 256 });
				current_row = (current_row + 1) % 80;
				break;

			default:
				break;
			}
		}

		for (const auto& i : elem) {
			c.WriteString(i.pos.row_, i.pos.col_, i.str, i.attr, Console::REVERSE_COLORS_HIGHLIGHT);
		}

		while (c.TimeElapsedSinceLastFlush() < 0.05f){
			_sleep(0);
		}
		c.Flush();
	}
}
